package com.kinghunt.misp.web.controller.userx;

import com.kinghunt.misp.common.dto.userx.WeChatSessionDTO;
import com.kinghunt.misp.common.param.userx.WeChatUserParam;
import com.kinghunt.misp.service.api.userx.UserWriteService;
import com.kinghunt.misp.service.api.userx.WeChatService;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.utils.Checker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序相关
 */
@RestController
@RequestMapping("/api/userx/wechat/")
@Slf4j
public class WeChatController {
    private final WeChatService weChatService;

    private final UserWriteService userWriteService;

    public WeChatController(WeChatService weChatService,
                            UserWriteService userWriteService) {
        this.weChatService = weChatService;
        this.userWriteService = userWriteService;
    }

    /**
     * 根据微信小程序登录code获取session信息
     *
     * @param code
     * @return
     */
    @GetMapping("code2Session")
    ApiResult<WeChatSessionDTO> code2Session(String code) {
        WeChatSessionDTO sessionDTO = Checker.success(weChatService.code2Session(code));
        return ApiResult.ok(sessionDTO);
    }

    /**
     * 微信小程序端注册新用户
     *
     * @param weChatUserParam
     * @return
     */
    @PostMapping("registerUserInWeChat")
    ApiResult<Long> registerUserInWeChat(@RequestBody WeChatUserParam weChatUserParam) {
        Long id = Checker.success(userWriteService.registerUserInWeChat(weChatUserParam));
        return ApiResult.ok(id);
    }


    @GetMapping("testRegister")
    ApiResult<Long> testRegister() {
        WeChatUserParam weChatUserParam = new WeChatUserParam();
        weChatUserParam.setUsername("099074217");
        weChatUserParam.setPhone("13275769162");
        weChatUserParam.setOpenId("ohKeq6076lHe7EEQ5KoaqjZiR7eY");
        weChatUserParam.setSessionKey("esK2I8I9fGk4h6lBW217og==");
        weChatUserParam.setOrganizationId(11L);
        Long id = Checker.success(userWriteService.registerUserInWeChat(weChatUserParam));
        return ApiResult.ok(id);
    }


    @GetMapping("registerUserInWeChatTest")
    ApiResult<Long> registerUserInWeChatTest() {
        WeChatUserParam weChatUserParam = new WeChatUserParam();
        weChatUserParam.setPhone("123");
        weChatUserParam.setOrganizationId(2L);
        weChatUserParam.setOpenId("xxx");
        weChatUserParam.setSessionKey("aaaaaaaaa");
        weChatUserParam.setAvatarUrl("https://xxxx");
        Long id = Checker.success(userWriteService.registerUserInWeChat(weChatUserParam));
        return ApiResult.ok(id);
    }

    /**
     * 获取微信授权手机号
     *
     * @param iv iv
     * @param encryptedData encryptedData
     * @param openId openId
     * @return
     */
    @GetMapping("getWeChatPhone")
    ApiResult<String> getWeChatPhone(String iv, String encryptedData, String openId) {
        String phone = Checker.success(weChatService.getWeChatPhone(iv, encryptedData, openId));
        return ApiResult.ok(phone);
    }
}
